package com.wannengbang.cloudleader.performance;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.base.BaseActivity;
import com.wannengbang.cloudleader.base.DataCallBack;
import com.wannengbang.cloudleader.bean.StatisticsBean;
import com.wannengbang.cloudleader.bean.UserInfoBean;
import com.wannengbang.cloudleader.homepage.AgentAllListActivity;
import com.wannengbang.cloudleader.homepage.AgentStandardListActivity;
import com.wannengbang.cloudleader.homepage.MerchantInfoListActivity;
import com.wannengbang.cloudleader.homepage.TransactionQueryActivity;
import com.wannengbang.cloudleader.performance.model.IPerformanceModel;
import com.wannengbang.cloudleader.performance.model.PerformanceModelImpl;
import com.wannengbang.cloudleader.utils.DateTimeUtil;
import com.wannengbang.cloudleader.utils.NumberFormatUtils;
import com.wannengbang.cloudleader.utils.SPManager;
import com.wannengbang.cloudleader.widget.ViewLoading;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PerformanceQueryActivity extends BaseActivity {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_order_fee_count_total)
    TextView tvOrderFeeCountTotal;
    @BindView(R.id.tv_look_details)
    TextView tvLookDetails;
    @BindView(R.id.tv_agent_count)
    TextView tvAgentCount;
    @BindView(R.id.ll_agent)
    LinearLayout llAgent;
    @BindView(R.id.tv_order_count)
    TextView tvOrderCount;
    @BindView(R.id.ll_order_count)
    LinearLayout llOrderCount;
    @BindView(R.id.tv_order_fee_count)
    TextView tvOrderFeeCount;
    @BindView(R.id.ll_transaction)
    LinearLayout llTransaction;
    @BindView(R.id.view_leftIcon)
    View viewLeftIcon;
    @BindView(R.id.ll_left)
    LinearLayout llLeft;
    @BindView(R.id.tv_AppTitle)
    TextView tvAppTitle;
    @BindView(R.id.tv_rightText)
    TextView tvRightText;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.tv_device_activation_num)
    TextView tvDeviceActivationNum;
    @BindView(R.id.ll_device_activation_num)
    LinearLayout llDeviceActivationNum;
    @BindView(R.id.tv_new_device_trade_sum)
    TextView tvNewDeviceTradeSum;
    @BindView(R.id.ll_new_device_trade_sum)
    LinearLayout llNewDeviceTradeSum;
    @BindView(R.id.tv_standard_agent_count)
    TextView tvStandardAgentCount;
    @BindView(R.id.ll_standard_agent)
    LinearLayout llStandardAgent;
    private String cycleType;
    private String param;
    private IPerformanceModel performanceModel;
    private UserInfoBean userInfoBean;
    private Calendar selectedDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_performance_query);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(false)
                .navigationBarDarkIcon(true)
                .navigationBarColor(R.color.white)
                .statusBarDarkFont(false, 0.2f)
                .init();
        initView();
        requestData();
    }

    public void initView() {
        selectedDate = Calendar.getInstance();
        cycleType = getIntent().getStringExtra("cycleType");
        param = getIntent().getStringExtra("param");
        performanceModel = new PerformanceModelImpl();
        userInfoBean = new Gson().fromJson(SPManager.getInstance().getUserInfoJson(), UserInfoBean.class);
        if (userInfoBean != null && userInfoBean.getData() != null) {
            tvName.setText(userInfoBean.getData().getName());
        }
        if ("date".equals(cycleType)) {
            tvAppTitle.setText("业绩(按日)");
        } else {
            tvAppTitle.setText("业绩(按月)");
        }
        tvRightText.setText(param);
        requestData();
    }

    public void requestData() {
        String paramValue = "";
        if (!TextUtils.isEmpty(param)) {
            paramValue = param.replace(".", "-");
        }
        ViewLoading.showProgress(mActivity);
        performanceModel.requestLineStatistics(cycleType, paramValue, new DataCallBack<StatisticsBean>() {
            @Override
            public void onSuccessful(StatisticsBean bean) {
                String orderFeeCountTotal = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getNew_order_sum()) / 100);
                tvOrderFeeCountTotal.setText(orderFeeCountTotal);
                tvAgentCount.setText(bean.getData().getNew_agent_count());
                tvOrderCount.setText(bean.getData().getNew_order_count());
                String orderFeeCount = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getNew_order_sum()) / 100);
                tvOrderFeeCount.setText(orderFeeCount);
                tvDeviceActivationNum.setText(bean.getData().getDevice_activation_num());
                String newDeviceTradeSum = NumberFormatUtils.formatTwoDecimal(Double.parseDouble(bean.getData().getNew_device_trade_sum()) / 100);
                tvNewDeviceTradeSum.setText(newDeviceTradeSum);
                tvStandardAgentCount.setText(bean.getData().getStandard_count());
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    private TimePickerView pvTime;

    public void selectTime() {
        boolean isSelectDay;
        if ("date".equals(cycleType)) {
            isSelectDay = true;
        } else {
            isSelectDay = false;
        }
        //这里可以更改参数，有好多类型
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        startDate.set(1900, 0, 1);
        endDate.set(2099, 11, 31);
        pvTime = new TimePickerBuilder(mActivity, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                if ("date".equals(cycleType)) {
                    param = DateTimeUtil.format13(date);
                } else {
                    param = DateTimeUtil.format22(date);
                }
                tvRightText.setText(param);
                requestData();
            }
        }).setType(new boolean[]{true, true, isSelectDay, false, false, false})
                .setSubmitColor(getResources().getColor(R.color.text_color_2))//确定按钮文字颜色
                .setSubCalSize(14)
                .setCancelColor(getResources().getColor(R.color.text_color_2))//取消按钮文字颜色
                .setTitleBgColor(getResources().getColor(R.color.white))
                .setDividerColor(getResources().getColor(R.color.line_color_gray))
                .setTextColorCenter(getResources().getColor(R.color.text_color_2))
                .setContentTextSize(15)
                .setLineSpacingMultiplier(2)
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window window = mDialog.getWindow();
            if (window != null) {
                window.setWindowAnimations(R.style.picker_view_slide_anim);//修改动画样式
                window.getDecorView().setPadding(0, 0, 0, 0);
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                lp.gravity = Gravity.BOTTOM;
            }
        }
        pvTime.show();
    }

    @OnClick({R.id.ll_left, R.id.ll_right, R.id.tv_look_details, R.id.ll_agent, R.id.ll_standard_agent, R.id.ll_order_count, R.id.ll_transaction, R.id.ll_device_activation_num})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_left:
                finish();
                break;
            case R.id.ll_right:
                if ("date".equals(cycleType)) {
                    selectedDate.setTime(DateTimeUtil.parse22(param));
                } else {
                    selectedDate.setTime(DateTimeUtil.parse23(param));
                }
                selectTime();
                break;
            case R.id.tv_look_details:
                Intent intent = new Intent(mActivity, TransactionQueryActivity.class);
                intent.putExtra("cycleType", cycleType);
                intent.putExtra("param", param);
                startActivity(intent);
                break;
            case R.id.ll_agent:
                intent = new Intent(mActivity, AgentAllListActivity.class);
                intent.putExtra("cycleType", cycleType);
                intent.putExtra("param", param);
                startActivity(intent);
                break;
            case R.id.ll_standard_agent:
                intent = new Intent(mActivity, AgentStandardListActivity.class);
                intent.putExtra("cycleType", cycleType);
                intent.putExtra("param", param);
                startActivity(intent);
                break;
            case R.id.ll_order_count:
                intent = new Intent(mActivity, TransactionQueryActivity.class);
                intent.putExtra("cycleType", cycleType);
                intent.putExtra("param", param);
                startActivity(intent);
                break;
            case R.id.ll_transaction:
                intent = new Intent(mActivity, PerformanceTypeActivity.class);
                intent.putExtra("cycleType", cycleType);
                intent.putExtra("param", param);
                startActivity(intent);
                break;
            case R.id.ll_device_activation_num:
                intent = new Intent(mActivity, MerchantInfoListActivity.class);
                intent.putExtra("cycleType", cycleType);
                intent.putExtra("param", param);
                startActivity(intent);
                break;
        }
    }
}
